package cn.iocoder.yudao.module.member.service.userinformation;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.member.controller.app.userinformation.vo.AppUserInformationPageReqVO;
import cn.iocoder.yudao.module.member.controller.app.userinformation.vo.AppUserInformationSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.userinformation.UserInformationDO;
import cn.iocoder.yudao.module.member.dal.mysql.userinformation.UserInformationMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.USER_INFORMATION_NOT_EXISTS;

/**
 * 用户身份资料列 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class UserInformationServiceImpl implements UserInformationService {

    @Resource
    private UserInformationMapper userInformationMapper;

    @Override
    public Long createUserInformation(AppUserInformationSaveReqVO createReqVO) {
        // 插入
        UserInformationDO userInformation = BeanUtils.toBean(createReqVO, UserInformationDO.class);
        userInformationMapper.insert(userInformation);
        // 返回
        return userInformation.getId();
    }

    @Override
    public void updateUserInformation(AppUserInformationSaveReqVO updateReqVO) {
        // 校验存在
        validateUserInformationExists(updateReqVO.getId());
        // 更新
        UserInformationDO updateObj = BeanUtils.toBean(updateReqVO, UserInformationDO.class);
        userInformationMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserInformation(Long id) {
        // 校验存在
        validateUserInformationExists(id);
        // 删除
        userInformationMapper.deleteById(id);
    }

    private void validateUserInformationExists(Long id) {
        if (userInformationMapper.selectById(id) == null) {
            throw exception(USER_INFORMATION_NOT_EXISTS);
        }
    }

    @Override
    public UserInformationDO getUserInformation(Long id) {
        return userInformationMapper.selectById(id);
    }

    @Override
    public PageResult<UserInformationDO> getUserInformationPage(AppUserInformationPageReqVO pageReqVO) {
        return userInformationMapper.selectPage(pageReqVO);
    }

}